package clothing.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class dbase {
	
	private String url = "jdbc:mysql://199.17.162.76/Clothing";
	private String driver = "com.mysql.jdbc.Driver";
	private String username = "root";
	private String password ="";
	
	Connection conn;
	
	public void open ()
	{
		
			try {
				Class.forName(driver)
				.newInstance();
				
				conn = DriverManager.getConnection(url, username, password);
				
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	public void test()
	{
		try {
			Class.forName(driver)
			.newInstance();
			
			conn = DriverManager.getConnection(url, username, password);
			
			Statement s = conn.createStatement();
			
			ResultSet rs = s.executeQuery("Select * from apple");
			
			while(rs.next())
			{
				System.out.println(rs.getString("Name")+"\t"+ rs.getString("time")
						+"\t"+ rs.getString("aid"));
			}
			
	
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void insertApple(String name, String desc)
	
	{
		this.open();
		try {
			Statement s = conn.createStatement();
			String query = "insert into apple values('','" + name + "',CURRENT_TIMESTAMP,'"+desc+"')";
		
			System.out.println(query);

			s.execute(query);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public int getCount(String query)
	{

		int count = 0;
		try {
			Class.forName(driver)
			.newInstance();
			
			conn = DriverManager.getConnection(url, username, password);
			
			Statement s = conn.createStatement();
			
			ResultSet rs = s.executeQuery(query);
			rs.next();
			count = rs.getInt(1);
			
	
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return count;
	}
	


	public static void main(String[] args){
		System.out.println(new dbase().getCount("select count(*) from apple"));
	}

	public Connection getConn() {
		return conn;
	}
}
